package com.kkshop.gwt.registration;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.kkshop.gwt.widget.wizard.impl.AbstractWizardPage;

public class AccountTypeWizardPage extends AbstractWizardPage {
    
    private Label roleLabel;
    private RadioButton customerButton;
    private RadioButton partnerButton;
    
    public AccountTypeWizardPage() {
        super(RegistrationController.getInstance().getResources().registerAccountTypeHeader());
    }

    public Widget createWidget() {
        VerticalPanel panel = new VerticalPanel();
        panel.add(getRoleLabel());
        panel.add(getCustomerButton());
        panel.add(getPartnerButton());
        return panel;
    }

    public boolean isFinished() {
        if(!getCustomerButton().isChecked() && !getPartnerButton().isChecked()) {
            setErrorMessage("Choose account type");
            return false;
        } else {
            setErrorMessage("");
            return true;
        }
    }
    
    private Label getRoleLabel() {
        if(roleLabel == null) {
            roleLabel = new Label("Type:");
        }
        return roleLabel;
    }

    private RadioButton getCustomerButton() {
        if(customerButton == null) {
            customerButton = new RadioButton("role", "Customer");
            customerButton.addClickListener(new ClickListener() {
                public void onClick(Widget widget) {
                    ((RegistrationWizard) getWizard()).getModel().setRole("CUSTOMER");
                    doUpdateButtons();
                }
            });
        }
        return customerButton;
    }

    private RadioButton getPartnerButton() {
        if(partnerButton == null) {
            partnerButton = new RadioButton("role", "Partner");
            partnerButton.addClickListener(new ClickListener() {
                public void onClick(Widget widget) {
                    ((RegistrationWizard) getWizard()).getModel().setRole("PARTNER");
                    doUpdateButtons();
                }
            });
        }
        return partnerButton;
    }
}
